package com.my.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.admin.entity.Comment;
import com.my.admin.service.CommentService;
import com.my.admin.util.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService service;

    @PostMapping("comment/get")
    public Rest get(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize, @RequestBody Comment comment) {
        return Rest.ok().setData("page", service.getList(new Page<>(pageNo, pageSize), comment));
    }

    @DeleteMapping("comments")
    public Rest del(@RequestParam Long id) {
        if(service.removeById(id)) {
            service.remove(new QueryWrapper<Comment>().eq("parent_id", id));
            return Rest.ok("删除成功！");
        }else {
            return Rest.err("删除失败！");
        }
    }

}
